package com.quartz.timertask;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import com.demo.common.model.QuartzJob;

import com.jfinal.log.Log;

public class QuartzManager {

	private static final Log logger = Log.getLog(QuartzManager.class);
	private Scheduler scheduler;

	/**
	 * @param scheduler
	 *            the scheduler to set
	 */
	public void setScheduler(Scheduler scheduler) {
		this.scheduler = scheduler;
	}

	/**
	 * 初始化任务调度
	 * 
	 * @author Joyce.Luo
	 * @date 2015-3-31 下午03:48:55
	 * @version V3.0
	 * @throws SchedulerException 
	 * @since Tomcat6.0,Jdk1.6
	 * @copyright Copyright (c) 2015
	 */
	@SuppressWarnings("unchecked")
	public void initJob(QuartzJob quartzJob, Class cls) throws SchedulerException {
		logger.info("初始化任务调度");
		
		TriggerKey triggerKey = TriggerKey.triggerKey(quartzJob.getJobName(), quartzJob.getJobGroup());
		CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
		if (null == trigger) {
			addQuartzJob(quartzJob, trigger, cls);
		}
		
	}

	/**
	 * 向任务调度中添加定时任务
	 * 
	 * @param job
	 *            定时任务信息
	 * @param trigger
	 *            定时调度触发器
	 * @author Joyce.Luo
	 * @date 2015-3-31 下午04:04:58
	 * @version V3.0
	 * @throws SchedulerException 
	 * @since Tomcat6.0,Jdk1.6
	 * @copyright Copyright (c) 2015
	 */
	@SuppressWarnings("unchecked")
	private void addQuartzJob(QuartzJob job, CronTrigger trigger, Class cls) throws SchedulerException {
		logger.info("向任务调度中添加定时任务");
		
		JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(job.getJobName(), job.getJobGroup()).build();
		jobDetail.getJobDataMap().put(job.getJobName(), job);
		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
		trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup())
				.withSchedule(scheduleBuilder).build();
		scheduler.scheduleJob(jobDetail, trigger);
		
	}

	/**
	 * 立即运行定时任务
	 * 
	 * @param job
	 *            定时任务信息
	 * @author Joyce.Luo
	 * @date 2015-4-20 下午02:08:41
	 * @version V3.0
	 * @since Tomcat6.0,Jdk1.6
	 * @copyright Copyright (c) 2015
	 */
	public void runJob(QuartzJob job) {
		logger.info("立即运行任务调度中的定时任务");
		try {
			if (null == job) {
				throw new RuntimeException("定时任务信息为空，无法立即运行");
			}
			JobKey jobKey = JobKey.jobKey(job.getJobName(), job.getJobGroup());
			if (null == jobKey) {
				throw new RuntimeException("任务调度中不存在[" + job.getJobName() + "]定时任务，不予立即运行！");
			}
			scheduler.triggerJob(jobKey);
		} catch (Exception e) {
			logger.error("立即运行任务调度中的定时任务异常！" + e.getMessage(), e);
			throw new RuntimeException(e.getMessage());
		}
	}

	/**
	 * 修改任务调度中的定时任务
	 * 
	 * @param job
	 *            定时任务信息
	 * @param triggerKey
	 *            定时调度触发键
	 * @param trigger
	 *            定时调度触发器
	 * @author Joyce.Luo
	 * @date 2015-3-31 下午04:16:54
	 * @version V3.0
	 * @since Tomcat6.0,Jdk1.6
	 * @copyright Copyright (c) 2015
	 */
	public void updateQuartzJob(QuartzJob job, TriggerKey triggerKey, CronTrigger trigger) {
		logger.info("修改任务调度中的定时任务");
		try {
			if (null == job || null == triggerKey || null == trigger) {
				logger.info("修改调度任务参数不正常！");
				return;
			}
			logger.info("原始任务表达式:" + trigger.getCronExpression() + "，现在任务表达式:" + job.getCronExpression());
			if (trigger.getCronExpression().equals(job.getCronExpression())) {
				logger.info("任务调度表达式一致，不予进行修改！");
				return;
			}
			logger.info("任务调度表达式不一致，进行修改");
			CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
			trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
			scheduler.rescheduleJob(triggerKey, trigger);
		} catch (Exception e) {
			logger.error("修改任务调度中的定时任务异常！" + e.getMessage(), e);
		}
	}

	/**
	 * 暂停任务调度中的定时任务
	 * 
	 * @param job
	 *            定时任务信息
	 * @author Joyce.Luo
	 * @date 2015-4-20 下午02:22:53
	 * @version V3.0
	 * @throws SchedulerException 
	 * @since Tomcat6.0,Jdk1.6
	 * @copyright Copyright (c) 2015
	 */
	public void pauseJob(QuartzJob job) throws SchedulerException {
		logger.info("暂停任务调度中的定时任务");
		if (null == job) {
			logger.info("暂停调度任务参数不正常！");
			return;
		}
		JobKey jobKey = JobKey.jobKey(job.getJobName(), job.getJobGroup());
		if (null == jobKey) {
			logger.info("任务调度中不存在[" + job.getJobName() + "]定时任务，不予进行暂停！");
			return;
		}
		scheduler.pauseJob(jobKey);
		
	}

	/**
	 * 恢复任务调度中的定时任务
	 * 
	 * @param job
	 *            定时任务信息
	 * @author Joyce.Luo
	 * @date 2015-4-20 下午02:26:08
	 * @version V3.0
	 * @throws SchedulerException 
	 * @since Tomcat6.0,Jdk1.6
	 * @copyright Copyright (c) 2015
	 */
	public void resumeJob(QuartzJob job) throws SchedulerException {
		logger.info("恢复任务调度中的定时任务");
		if (null == job) {
			logger.info("恢复调度任务参数不正常！");
			return;
		}
		JobKey jobKey = JobKey.jobKey(job.getJobName(), job.getJobGroup());
		if (null == jobKey) {
			logger.info("任务调度中不存在[" + job.getJobName() + "]定时任务，不予进行恢复！");
			return;
		}
		scheduler.resumeJob(jobKey);
	}

	/**
	 * 删除任务调度中的定时任务
	 * 
	 * @param job
	 *            定时任务信息
	 * @author Joyce.Luo
	 * @date 2015-3-31 下午04:30:03
	 * @version V3.0
	 * @throws SchedulerException 
	 * @since Tomcat6.0,Jdk1.6
	 * @copyright Copyright (c) 2015
	 */
	public void deleteJob(QuartzJob job) throws SchedulerException {
		logger.info("删除任务调度中的定时任务");
		if (null == job) {
			logger.info("删除调度任务参数不正常！");
			return;
		}
		JobKey jobKey = JobKey.jobKey(job.getJobName(), job.getJobGroup());
		if (null == jobKey) {
			logger.info("任务调度中不存在[" + job.getJobName() + "]定时任务，不予进行删除！");
			return;
		}
		scheduler.deleteJob(jobKey);
	}

	/**
	 * 删除任务调度定时器
	 * 
	 * @param triggerKey
	 * @author Joyce.Luo
	 * @date 2015-3-31 下午04:35:33
	 * @version V3.0
	 * @since Tomcat6.0,Jdk1.6
	 * @copyright Copyright (c) 2015
	 */
	void deleteJob(TriggerKey triggerKey) {
		logger.info("删除任务调度定时器");
		try {
			if (null == triggerKey) {
				logger.info("停止任务定时器参数不正常，不予进行停止！");
				return;
			}
			logger.info("停止任务定时器");
			scheduler.pauseTrigger(triggerKey);
			scheduler.unscheduleJob(triggerKey);
		} catch (Exception e) {
			logger.info("删除任务调度定时器异常！" + e.getMessage(), e);
		}
	}
	
	public void start() throws SchedulerException {
		scheduler.start();
	}
	
	public void shutdown() throws SchedulerException {
		scheduler.shutdown();
	}
}
